<template>
  <el-upload ref="uploadRef" :show-file-list="false" class="upload-demo" :action="action" :before-upload="beforeAvatarUpload" :on-success="handleAvatarSuccess"> <el-button type="primary" size="small">导入excel</el-button> </el-upload>
</template>
<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { inject, ref } from 'vue'
const reload = inject('reload')
const props = defineProps({
  action: {
    type: String,
    default: '/api/excel/test',
  },
})
const uploadRef = ref()

const beforeAvatarUpload = (rawFile) => {
  const suffix = rawFile.name.substr(rawFile.name.lastIndexOf('.'))
  if (suffix !== '.xlsx') {
    ElMessage.error('请导入正确的excel文件')
    return false
  }
  return true
}
const handleAvatarSuccess = (response, uploadFile) => {
  if (response.code != 200) return ElMessage.error(response.msg)
  ElMessage.success('导入成功')
  reload()
}
</script>
<style lang="scss" scoped>
.upload-demo {
  display: inline-block;
  margin-left: 12px;
}
</style>
